﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fis.Win.Utils;
using Agile.Engine.Utils;

namespace Fis.Win.Forms
{
    public partial class BillQueryForm : BaseForm
    {
        Dictionary<string, DevExpress.XtraEditors.LookUpEdit> registry
            = new Dictionary<string, DevExpress.XtraEditors.LookUpEdit>();

        public BillQueryForm()
        {
            InitializeComponent();

            #region Lookup Registe
            registry["0"] = lookUpEdit0;
            registry["1"] = lookUpEdit1;
            registry["2"] = lookUpEdit2;
            registry["3"] = lookUpEdit3;
            registry["4"] = lookUpEdit4;
            registry["5"] = lookUpEdit5;
            registry["6"] = lookUpEdit6;
            registry["7"] = lookUpEdit7;
            registry["8"] = lookUpEdit8;
            #endregion

            InitializeViewUI();
            InitializeBinding();
        }

        void InitializeAccountEdit(DevExpress.XtraEditors.LookUpEdit edit)
        {
            edit.Properties.ShowHeader = true;
            edit.Properties.Columns.AddRange(
                new DevExpress.XtraEditors.Controls.LookUpColumnInfo[]{
                     new DevExpress.XtraEditors.Controls.LookUpColumnInfo("FCODE","科目编码")
                    ,new DevExpress.XtraEditors.Controls.LookUpColumnInfo("FNAME","科目名称")
                    //,new DevExpress.XtraEditors.Controls.LookUpColumnInfo("FRATECODE","币种编码")
                    //,new DevExpress.XtraEditors.Controls.LookUpColumnInfo("FRATENAME","币种名称")
                });

            edit.Properties.DataSource = CacheContext.LowerAccountCache();
        }


        public override void InitializeViewUI()
        {
            #region 辅助核算
            var aids = CacheContext.GetCache("aidsgroup");
            for (var i = 0; i < 9; i++)
            {
                var item = layoutControl1.GetItemByControl(registry[i.ToString()]);
                var group = aids.Select(string.Format("key='{0}'", i));
                if (group.Length > 0)
                {
                    var key = group[0]["key"].ToString();
                    var caption = group[0]["caption"].ToString();

                    item.Text = caption;
                    item.CustomizationFormText = caption;
                    var edit = registry[i.ToString()];
                    edit.Tag = key;

                }
                else
                {
                    layoutControl1.HideItem(item);
                    registry.Remove(i.ToString());
                }
            }
            #endregion

        }

        public override void InitializeBinding()
        {
            var range = DataUtils.Parse(1, AppSession.Book["FBOOKRANGE"]);
            if (range > 12)
                range = 12;
            else if (range < 1)
                range = 1;
            edDateStart.EditValue = DateTime.Parse(string.Format("{0}-{1}-1", AppSession.Year, range));
            edDateStart.Properties.MinValue = DateTime.Parse(string.Format("{0}-1-1", AppSession.Year));
            edDateStart.Properties.MaxValue = DateTime.Parse(string.Format("{0}-12-31", AppSession.Year));
            if (range >= 12)
                edDateEnd.EditValue = DateTime.Parse(string.Format("{0}-1-1", AppSession.Year)).AddYears(1).AddMinutes(-1);
            else
                edDateEnd.EditValue = DateTime.Parse(string.Format("{0}-{1}-1", AppSession.Year, range + 1)).AddMinutes(-1);
            edDateEnd.Properties.MinValue = edDateStart.Properties.MinValue;
            edDateEnd.Properties.MaxValue = edDateStart.Properties.MaxValue;

            InitializeAccountEdit(edAccount);

            edState.Properties.DataSource = Fis.Win.Utils.CacheContext.BillStatusCache();
            edState.Properties.DisplayMember = "display";
            edState.Properties.ValueMember = "value";

            UIUtils.LookUpBinding(edEnter, CacheContext.GetCache("TUSER"), "FUSERNAME", "SID");
            UIUtils.LookUpBinding(edAudit, CacheContext.GetCache("TUSER"), "FUSERNAME", "SID");

            foreach (var i in registry.AsEnumerable())
            {
                var s = CacheContext.GetCache("TAIDS").Select(string.Format("FGROUP='{0}'", i.Key));
                if (s.Length > 0)
                {
                    UIUtils.LookUpBinding(i.Value, s.CopyToDataTable(), "FNAME", "SID");
                }
            }

        }

        public string FilterMaster
        {
            get
            {
                var filter = new StringBuilder();

                //制单日期
                filter.Append(UIUtils.ToFilterString(edDateStart, " AND m.FENTERDATE>='{0}'"));
                filter.Append(UIUtils.ToFilterString(edDateEnd, " AND m.FENTERDATE<='{0}'"));
                //状态
                filter.Append(UIUtils.ToFilterString(edState, " AND m.FSTATUS in ({0})"));
                //顺序号
                filter.Append(UIUtils.ToFilterString(edNoStart, " AND m.FSEQUENCE >= {0}"));
                filter.Append(UIUtils.ToFilterString(edNoEnd, " AND m.FSEQUENCE <= {0}"));
                //用户
                filter.Append(UIUtils.ToFilterString(edEnter, " AND m.FENTER='{0}'"));
                filter.Append(UIUtils.ToFilterString(edAudit, " AND m.FCHECKED='{0}'"));

                return filter.ToString();
            }
        }

        public string FilterDetail
        {
            get
            {
                var filter = new StringBuilder();
                //会计科目
                filter.Append(UIUtils.ToFilterString(edAccount, " AND d.FACCOUNTCODE='{0}'"));
                //辅助核算
                var fzhs = from i in registry.AsEnumerable()
                           where !StringUtils.IsEmpty(i.Value.EditValue)
                           select string.Format(" AND d.FHSVALUE LIKE '%{0}:{1}%'", i.Key, i.Value.EditValue);

                filter.Append(string.Join(" ", fzhs.ToArray()));

                return filter.ToString();
            }
        }

        private void bnOK_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private void bnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }


    }
}
